package com.jhws.business.car.api;

import com.jhws.business.car.bo.*;
import com.jhws.business.car.bo.api.ApiCarEnteringInfo;
import com.jhws.business.car.bo.api.ApiParkInfo;
import com.jhws.business.car.bo.api.ApiPaymentVerification;
import com.jhws.business.car.service.CarEnteringRecordServic;
import com.jhws.business.car.service.CarInfoService;
import com.jhws.business.car.service.CarPaymentService;
import com.jhws.business.car.service.ParkingLotService;
import com.jhws.common.base.bo.JsonRespData;
import com.jhws.util.DateUtil;
import com.jhws.util.StringUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/car")
public class CarApi {
    @Resource
    private CarEnteringRecordServic carEnteringRecordServic;
    @Resource
    private ParkingLotService parkingLotService;
    @Resource
    private CarInfoService carInfoService;
    @Resource
    private CarPaymentService carPaymentService;


    @RequestMapping(value = "anon/parkInfo", consumes = "application/json")
    @ResponseBody
    public JsonRespData parkInfo(@RequestBody ApiParkInfo parkInfoApi) {
        ParkingLot parkingLot = getParkingLot(parkInfoApi.getParkId());
        if (StringUtils.isEmpty(parkInfoApi.getParkId()) || parkingLot == null) {
            return new JsonRespData("", -1, "没有此停车场！");
        }
        parkingLot.setParkSpaceNum(parkInfoApi.getParkSpaceNum());
        parkingLot.setMonthCardNum(parkInfoApi.getMonthCardNum());
        parkingLot.setGuestNum(parkInfoApi.getGuestNum());
        parkingLotService.saveParkingLot(parkingLot);
        return new JsonRespData("", 0, "成功");
    }

    @RequestMapping(value = "anon/carEnteringInfo", consumes = "application/json")
    @ResponseBody
    public JsonRespData carEnteringInfo(@RequestBody ApiCarEnteringInfo carEnteringInfoApi) throws InvocationTargetException, IllegalAccessException {
        CarEnteringRecord carEnteringRecord = new CarEnteringRecord();
        BeanUtils.copyProperties(carEnteringRecord, carEnteringInfoApi);// 属性拷贝
        ParkingLot parkingLot = getParkingLot(carEnteringRecord.getParkId());
        if (StringUtils.isEmpty(carEnteringRecord.getParkId()) || parkingLot == null) {
            return new JsonRespData("", -1, "没有此停车场！");
        }

        if (CarConstant.CAR_BUSSINESS_OUT_TYPE == carEnteringInfoApi.getBussinessType()) {
            CarInfo carInfo = getCarInfo(carEnteringRecord.getParkId(), carEnteringInfoApi.getPlateNumber());
            if (carInfo != null && carInfo.getCardType() == CarConstant.CAR_TEMP_CARD) {
                carInfoService.deleteCarInfo(carInfo);
            }
        }

        carEnteringRecordServic.saveCarEntering(carEnteringRecord);
        parkingLot.setMonthCardRemainNum(carEnteringInfoApi.getMonthCardRemainNum());
        parkingLot.setGuestRemainNum(carEnteringInfoApi.getGuestRemainNum());
        parkingLotService.saveParkingLot(parkingLot);
        return new JsonRespData("", 0, "成功");
    }

    @RequestMapping(value = "anon/parkPayment")
    @ResponseBody
    public JsonRespData ParkPayment(CarPaymentRecord carPaymentRecord) {
        ParkingLot parkingLot = getParkingLot(carPaymentRecord.getParkId());
        if (StringUtils.isEmpty(carPaymentRecord.getParkId()) || parkingLot == null) {
            return new JsonRespData("", -1, "没有此停车场！");
        }
        if (StringUtils.isEmpty(carPaymentRecord.getPlateNumber())) {
            return new JsonRespData("", -1, "车牌号错误！");
        }
        CarInfo carInfo = getCarInfo(carPaymentRecord.getParkId(), carPaymentRecord.getPlateNumber());
        if (carInfo == null) {
            carInfo = new CarInfo();
        }
        carInfo.setCarType(carPaymentRecord.getCarType());
        carInfo.setOwnerCode(carPaymentRecord.getOwnerCode());
        carInfo.setParkId(carPaymentRecord.getParkId());
        carInfo.setPlateNumber(carPaymentRecord.getPlateNumber());
        if (carPaymentRecord.getPaymentType() == CarConstant.CAR_PAYMENT_TYPE_TEMP) {
            long currentTime = System.currentTimeMillis();
            currentTime += 30 * 60 * 1000;
            Date date = new Date(currentTime);
            carInfo.seteStartTime(new Date());
            carInfo.seteEndTime(date);
            carInfo.setCardType(3);
        } else if (carPaymentRecord.getPaymentType() == CarConstant.CAR_PAYMENT_TYPE_CARD) {
            carInfo.setCardType(1);
            if (carInfo.geteStartTime() == null) {
                carInfo.seteStartTime(new Date());
            }
        } else {
            return new JsonRespData("", -1, "缴费类型错误！");
        }
        carInfoService.saveCarInfo(carInfo);
        carPaymentService.savePaymentRecord(carPaymentRecord);
        return new JsonRespData("", 0, "成功");
    }

    @RequestMapping("anon/paymentVerification")
    @ResponseBody
    public JsonRespData paymentVerification(@RequestBody ApiPaymentVerification apiPaymentVerification) {
        ParkingLot parkingLot = getParkingLot(apiPaymentVerification.getParkId());
        if (StringUtils.isEmpty(apiPaymentVerification.getParkId()) || parkingLot == null) {
            return new JsonRespData("", -1, "没有此停车场！");
        }
        String date = DateUtil.getTimeNowFmtSec();
        List<CarInfo> carInfos = carInfoService.queryCarInfoByWhere(" where park_id ='" + apiPaymentVerification.getParkId() + "' and plate_number = '" + apiPaymentVerification.getPlateNumber() + "' and '" + date + "' <= e_end_time and '" + date + "' >= e_start_time ");
        if (carInfos != null && carInfos.size() > 0) {
            return new JsonRespData("", 0, "验证成功");
        }
        return new JsonRespData("", -1, "验证失败！");
    }

    @RequestMapping("anon/getParkInfo")
    @ResponseBody
    public JsonRespData getParkInfo(String commCode) {
        return new JsonRespData("", 1, parkingLotService.queryParkingLostByWhere("where 1=1 and comm_code = '" + commCode + "'"));
    }

    @RequestMapping("anon/getParkInfoByID")
    @ResponseBody
    public JsonRespData getParkInfoByID(String parkId) {
        return new JsonRespData("", 1, getParkingLot(parkId));
    }

    private ParkingLot getParkingLot(String parkId) {
        return parkingLotService.queryParkingLotInfo(parkId);
    }

    private CarInfo getCarInfo(String parkId, String plateNumber) {
        List<CarInfo> carInfos = carInfoService.queryCarInfoByWhere(" where park_id ='" + parkId + "' and plate_number = '" + plateNumber + "'");
        return carInfos != null && carInfos.size() > 0 ? carInfos.get(0) : null;
    }


}
